package com.quasar.hpatient.module.mine_case;

import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.ImageView;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.mine_case.MineCaseBean;
import com.quasar.hpatient.module.mine_case_edit.MineCaseEditActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.recycler.BaseCommonAdapter;
import lib.quasar.recycler.BaseCommonMultAdapter;
import lib.quasar.recycler.decoration.SpaceDecoration;
import lib.quasar.recycler.holder.RecyclerHolder;
import lib.quasar.recycler.manager.CrashLinearLayoutManager;
import lib.quasar.recycler.model.MultModel;
import lib.quasar.util.DimenUtil;
import lib.quasar.util.LogUtil;
import okhttp3.RequestBody;

/**
 * description: 我 ==> 我的档案 ==> 我的病例
 * created by kalu on 2018/7/16 17:14
 */
public final class MineCasePresenter implements BasePresenter {

    private final ArrayList<MineCaseBean> mDatas = new ArrayList<>();

    final void initList(final MineCaseView view, final RecyclerView recycler) {

        if (null == recycler || null == view)
            return;

        final BaseCommonMultAdapter<MineCaseBean> adapter = new BaseCommonMultAdapter<MineCaseBean>(mDatas) {

            @Override
            protected void onMult() {
                addMult(MultModel.TYPE_0, R.layout.activity_mine_case_type_list);
                addMult(MultModel.TYPE_1, R.layout.activity_mine_case_type_text);
            }

            @Override
            protected void onNext(RecyclerHolder holder, final MineCaseBean model, final int position) {

                view.setText(holder, R.id.mine_case_hispital, model.getHospital_name());
                view.setText(holder, R.id.mine_case_date, model.getDates());
                view.setText(holder, R.id.mine_case_name, model.getMedical_title());
                view.setText(holder, R.id.mine_case_info, model.getJson_content());

                if (model.isPicture()) {
                    final RecyclerView picture = holder.getView(R.id.mine_case_list);
                    final List<MineCaseBean.MinePictureBean> list = model.getList();
                    if (null == picture.getAdapter()) {
                        initPicture(view, picture, list, position);
                    } else {
                        ((BaseCommonAdapter) picture.getAdapter()).clearInsertData(list);
                    }
                }

                holder.setOnClickListener(R.id.mine_case_edit, v -> {

                    final Intent intent = new Intent(BaseApp.getContext(), MineCaseEditActivity.class);
                    intent.putExtra(MineCaseEditActivity.CASE_ID, model.getId());
                    intent.putExtra(MineCaseEditActivity.CASE_NAME, model.getHospital_name());
                    intent.putExtra(MineCaseEditActivity.CASE_DATE, model.getDates());
                    intent.putExtra(MineCaseEditActivity.CASE_TITLE, model.getMedical_title());
                    intent.putExtra(MineCaseEditActivity.CASE_INSPECTION, model.getJson_content());
                    intent.putStringArrayListExtra(MineCaseEditActivity.CASE_PICTURE, model.getJson_pic());
                    view.goNextResult(intent, MineCaseActivity.REQUEST_CODE);
                });
            }
        };

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        view.setRecycler(recycler, adapter, manager);
    }

    final void caculDate(final MineCaseView view, final RecyclerView recycler, final String str) {

        if ("一周".equals(str)) {
            final Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -7);
            Date d = c.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            caseList(view, recycler, getDate(), format.format(d));
        } else if ("两周".equals(str)) {
            final Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.DATE, -14);
            Date d = c.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            caseList(view, recycler, getDate(), format.format(d));
        } else if ("一月".equals(str)) {
            final Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -1);
            Date d = c.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            caseList(view, recycler, getDate(), format.format(d));
        } else if ("三月".equals(str)) {
            final Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -3);
            Date d = c.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            caseList(view, recycler, getDate(), format.format(d));
        } else if ("半年".equals(str)) {
            final Calendar c = Calendar.getInstance();
            c.setTime(new Date());
            c.add(Calendar.MONTH, -6);
            Date d = c.getTime();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            caseList(view, recycler, getDate(), format.format(d));
        } else {
            caseList(view, recycler, null, null);
        }
    }

    final void caseList(final MineCaseView view, final RecyclerView recycler, final String beginDate, final String endDate) {

        final HashMap<String, Object> map = new HashMap<>();
        map.put(HttpParams.BEGIN_DATE, TextUtils.isEmpty(beginDate) ? getDate() : getDate(beginDate));
        map.put(HttpParams.END_DATE, TextUtils.isEmpty(endDate) ? "1970-01-01 00:00:00" : getDate(endDate));

        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_1_17);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().mineCaseList(params);

        request(observable, new OnModelAcceptChangeListener<List<MineCaseBean>>() {
            @Override
            public void modelSucc(List<MineCaseBean> result) {

                if (null == result)
                    return;

                mDatas.clear();
                mDatas.addAll(result);
                view.refreshList(recycler);
            }
        });
    }

    final void refreshDate(final MineCaseView view, final RecyclerView recycler, final ArrayList<String> list,
                           final int caseId, final String caseTitle, final String hospitalName, final String caseDate, final String caseInspection) {

        if (caseId == -1) {

            final MineCaseBean news = new MineCaseBean();
            news.setJson_pic(list);
            news.setId(caseId);
            news.setJson_content(caseInspection);
            news.setMedical_title(caseTitle);
            news.setHospital_name(hospitalName);
            news.setMedical_history_date(caseDate);
            mDatas.add(0, news);
        } else {

            for (MineCaseBean temp : mDatas) {
                if (temp.getId() == caseId) {
                    temp.setJson_pic(list);
                    temp.setId(caseId);
                    temp.setJson_content(caseInspection);
                    temp.setMedical_title(caseTitle);
                    temp.setHospital_name(hospitalName);
                    temp.setMedical_history_date(caseDate);
                    break;
                }
            }
        }
        view.refreshList(recycler);
    }

    private final void initPicture(final MineCaseView view, final RecyclerView recycler, final List<MineCaseBean.MinePictureBean> list, int index) {

        if (null == recycler || null == view)
            return;

        final BaseCommonAdapter<MineCaseBean.MinePictureBean> adapter = new BaseCommonAdapter<MineCaseBean.MinePictureBean>(list, R.layout.activity_mine_case_type_list_picture) {

            @Override
            protected void onNext(RecyclerHolder holder, final MineCaseBean.MinePictureBean model, final int position) {

                final ImageView image = holder.getView(R.id.mine_case_type_list_picture);
                final String url = model.getUrl();
                view.setImage(image, url);

                image.setOnClickListener(v -> {

                    final LinkedList<ImageView> list1 = new LinkedList<>();
                    for (int i = 0; i < mDatas.size(); i++) {
                        list1.add(image);
                    }
                    view.showPhoto(position, list, list, list1);
                });
            }
        };

        final CrashLinearLayoutManager manager = new CrashLinearLayoutManager(BaseApp.getContext());
        final SpaceDecoration decoration = new SpaceDecoration(DimenUtil.dp2px(10), Color.TRANSPARENT);
        manager.setOrientation(RecyclerView.HORIZONTAL);
        view.setRecycler(recycler, adapter, manager, decoration);
    }

    @Override
    public void recycler() {
    }
}
